home *** CD-ROM | disk | FTP | other *** search
- Path: sullivan.ucc.hull.ac.uk!newsmaster@hull.ac.uk
- From: "J.D.Hoyland" <J.D.Hoyland@apphys.hull.ac.uk>
- Newsgroups: comp.lang.c++
- Subject: Re: Newbie question about function calling.
- Date: 12 Mar 1996 13:26:28 GMT
- Organization: University of Hull
- Message-ID: <4i3u25$6ev@sullivan.ucc.hull.ac.uk>
- References: <4hohd3$r9@brickbat.mindspring.com>
- NNTP-Posting-Host: fowler.hull.ac.uk
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (X11; I; SunOS 4.1.3 sun4m)
- X-URL: news:4hohd3$r9@brickbat.mindspring.com
-
- wdnick@mindspring.com (William "Doug" Nicholson) wrote:
- >I'm having a problem somewhere between the following two functions.
- >My main calls the function 'counter' (shown below) which in turn calls
- >'getinfo' by trying to assign 'getinfo's return value to the string
- >variable 'filename'. 'Getinfo' prompts the user to enter a filename
- >and stores it in the string 'infile'. I've added a couple of lines
- >below and started them in my post with the > so they will stand out.
- >In the first one (in the getinfo function), the printf line prints
- >whatever I type in at the prompt. Theoretically this should be passed
- >to 'filename' in the other function. Well, the printf that tries to
- >print "FILE = 'filename'" stops after "FILE =" and the computer locks
- >up.
- >
- >Anybody got any idea what's happening here? Have I overlooked a
- >really stupid thing or what?
- >
- >Thanks for any help. The problem area is listed below.
-
- This is an arrays and pointers problem, and is difficult to get your head round
- at first.
-
- First of all the variable type char is not a string variable. a single variable
- of type char holds an integer value between 0 and 255. Functions like printf
- and scanf interpret this value as a member of the ASCII character set and allow
- you to store a character of text. Your function getinfo() as it stands at the
- moment has a return value of type char. Which means it expects to return one
- single character. Your putting infile[15] in the return statement is obviously
- giving it a headache.
- The problem is infile[15] is NOT the name of the variable containing the string
- you entered this is what happens:
-
-
- >
- >char getinfo(void)
- >/*****************************************************************
- > * This function prompts the user for a filename and returns it. *
- > *****************************************************************/
- >{
- > char infile[15];
-
- The above line declares a 15 element array of chars. the identifier 'infile'
- is a pointer to the first element of this array.
-
- >
- > printf("Enter the path and filename of a file to process ");
- > printf("or enter XX to quit >");
- > scanf("%s", infile);
-
- This is fine scanf() takes the memory address of the array given it by 'infile'
- and puts characters there
-
- >
- >> printf("FILE = %s\n\n", infile);
- >
- > return(infile[15]);
- >} /* end function getinfo */
-
- This is where things fall appart. infile[15] is not the string. When you
- create an array, you can assign values to it's individual elements by using
- the subscript opperator (the [] bit). So for instance if you have an arry
-
- like this:
- sometype array[5];
-
- you can say:
-
- array[2] = 4;
- array[1] = 3;
-
- you can pass these elements in functions:
-
- void somefunc(sometype st1, sometype st2);
- :
- :
- somefunc(array[2],array[1]);
-
- and return them from functions:
-
- sometype somefunc()
- {
- :
- :
- return(array[3]);
- }
-
- But this is not returning the array it is returning the value of element 3 of
- the array.
-
- So above when you say return(infile[15]);
- You are trying to retrun the value of element id 15 of the array infile;
- This will cause fatal errors because there is no element id 15!!!
-
- When you declare an array : sometype array[n];
- it has elements called: array[0],array[1],array[2] .... array[n-1]!
- So your infile array has elements 0 to 14;
-
- Further more when, in the next function you say filename[15]=getinfo();
- you are trying to assign the returned value of getinfo to the 16th(id15)
- element
- of the array filename. This will deffinitly cause problems because the returned
- value is then placed in memory AFTER the end of the array, which may well
- overwrite other important information in memory.
-
- Whilst this is probably what's causing the computer to lock, you still need to
- radicaly change the function to get the desired effect. There is no way you can
- return the entire array in the way you want to, when the functions return type
- is just char.
-
- What I would do is this:
- change your function return type to void and give it a char* (pointer to char)
- parameter. Pass filename to it and get rid of char infile[15] altogether;
-
- so getinfo will now be:
-
-
- void getinfo(char* infile)
- >{
- >
- > printf("Enter the path and filename of a file to process ");
- > printf("or enter XX to quit >");
- > scanf("%s", infile); /* scanf now writes strait to the memory
- location you sent it i.e. filename */
- >
- >> printf("FILE = %s\n\n", infile);
- >
- >
- >} /* end function getinfo */
-
-
-
- >
- >int counter(void)
- >{
- > int i, /* lengths of triangle sides */
- > count[128]; /* character count array */
- >
- > char chr,
- > filename[15] = "",
- > foo[2] = "1";
- >
- > FILE *inp, /* input file pointer */
- > *outp; /* output file pointer */
- >
- > getinfo(filename); /* passing filename passes the address of the
- start of the array (string)*/
- >
- >> printf("FILE = %s\n\n", filename);
- >
- >
-
- This should work. There are other ways around the problem using the new
- operator
- etc. but the main thing to remember is there is no such thing as a string in
- C/C++, only arrays of type char. When you write:
-
- sometype array[15]; you are declaring an array of type sometype with 15
- elements
-
- and when you write:
-
- array[15] in any other circumstance other than a declaration, you are
- specifying
- the 16th element of the array.
-
- Hope this helps.
-
-
- James :)
-
-
- P.S. I think I actually posted this reply once by accident befor I finished so
- ignore earlier stuff!!!
-
-